
use "processed/rpisave/rpisum1974.dta", clear
forvalues n = 1975/2017 {
append using "processed/rpisave/rpisum`n'.dta" 

}


**RPI merge
gen erpi1 = bread+ cereals + biscuits

*2. Meat_Fish 
gen erpi2 = beef+ lamb+pork+bacon+poul_oth +poultry+oth_meat+fish

*3. Milk_Eggs 
gen erpi3 = butter+ cheese +eggs + milkfres +milkprod

*4 Oilfats
gen erpi4 = oil_fats

*5 Fruit 
gen erpi5 =fruit

*6 Vegetables
gen erpi6 = potatoes + oth_vegs

*7 otherfood
gen erpi7 = swe_choc + sug_pres + oth_food

*8 SOFTDRINK
gen erpi8 = tea +  coffee + softdrin
*gen sumFood =  FOOD
gen sumFooddis = erpi1 + erpi2 + erpi3 + erpi4 + erpi5 + erpi6 + erpi7 + erpi8
* 9   TOBACCO
*gen erpi9 = offwinsp + offbeer + TOBACCO
gen erpi9 = TOBACCO

* 10 CATERING + ALCOHOL
gen erpi10 = ALCOHOL + CATERING


* 11 Housing
gen erpi11 = HOUSING + FUEL_LIG - mortgageint - insgr

* 12 Clothing
gen erpi12 = CLOTH_SH

* 13 HHG
gen erpi13 = HHGOODS + domservs 
* 14. Communication: 
gen erpi14 = postage + telephon + telemess
* 15 PG_S
gen erpi15 = P_GD_SER + fees_sub + insgr
* 16 Transport
gen erpi16 = MOTORING + FARESET
* 17 LGS:
gen erpi17 = LGOODS + LSERVS

gen Irpi = totexp - mortgageint
gen I =  totexp
drop if I < 0
drop if I ==.
drop if quant == .

**Representative Aggregation*****
bysort quant datayear: egen double I_REP = sum(I)
bysort quant datayear: egen double I_REPrpi = sum(Irpi)
bysort quant datayear: egen double num_hh = count(weight)
bysort quant datayear: egen double I_REPw = sum(I*weight)
bysort quant datayear: egen double I_REPrpiw = sum(Irpi*weight)
bysort quant datayear: egen double num_hhw = sum(weight)
bysort quant datayear: egen double num_hhw_pre = sum(weight_pre)
bysort quant datayear: egen double I_REPw_pre = sum(I*weight_pre)
bysort quant datayear: egen double ave_numpeeps = mean(numpeeps)


format I_REPw I_REPw_pre %12.0f

replace I_REPw = I_REPw_pre if datayear < 1997

gen PC_per = I_REP/num_hh
replace  PC_per = I_REPw/num_hhw if year > 1997

forvalues x = 1/17	 {

bysort quant datayear: egen erpi`x'_REP = total(erpi`x')
bysort quant datayear: egen double  erpi`x'_REPw = total(erpi`x'*weight)
format erpi`x'_REP %12.0f
format erpi`x'_REPw %12.0f

gen s_RPI_`x'_REP1 = erpi`x'_REP/I_REPrpi

gen s_RPI_`x'_REP2 = erpi`x'_REPw/I_REPrpiw

}


forvalues x = 1/17	 {
gen s_RPI_`x' = s_RPI_`x'_REP1 
replace s_RPI_`x'  = s_RPI_`x'_REP2 if datayear > 1996
}



duplicates drop datayear quant,force
keep s_RPI_1 s_RPI_2 s_RPI_3 s_RPI_4 s_RPI_5 s_RPI_6 s_RPI_7 s_RPI_8 s_RPI_9 s_RPI_10 s_RPI_11 s_RPI_12 s_RPI_13 s_RPI_14 s_RPI_15 s_RPI_16 s_RPI_17 PC_per datayear quant

xtset datayear quant 
save processed\RPIdeci,replace

merge m:1 datayear using processed/pricemerge_dis.dta

egen groupid = group(datayear quant)

reshape long s_RPI_ price, i(groupid) j(good)
egen groupid2 = group(good quant)
xtset groupid2 datayear 

g logp = log(price)
g logb = log(s_RPI_) 

g dlogp = logp - l.logp
g dlogb = logb - l.logb

g btorn = (l.s_RPI_ + s_RPI_)/2

bysort quant datayear: egen dlop_tot = total(dlogp*btorn)

duplicates drop quant datayear,force

keep datayear	quant dlop_tot

reshape wide dlop_tot, i(datayear)  j(quant)

preserve 
keep if datayear > 2004

forvalues x = 1/10	 {
egen mean_dlop_tot`x' = mean(dlop_tot`x'*100)
gen ave_dlop`x' = round(mean_dlop_tot`x',0.1)
}
keep if datayear ==2017
keep ave_dlop*
drop ave_dlop1	ave_dlop10
outsheet using DataforMatlab\A1mircoONS.csv, comma replace
restore

forvalues x = 1/10	 {
gen cum_dlop_tot`x' = sum(dlop_tot`x')
}

keep cum* 
outsheet using DataforMatlab\dec_torn.csv, comma replace nonames


*by datayear: egen a = sum(dlop_tot) 
*outsheet using DataforMatlab\JLdata.csv, comma replace nonames
